<?php

/**
 * model_grade
 */

class Model_grade extends YOU {

	public function __construct()
	{
		parent::__construct();
	}

	/** 读取总数 **/
	public function count()
	{
		$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_grade_up` WHERE is_now=1 ");

		$count = (int)$row['count'];

		return $count;
	}

	/** 读取成配置 **/
	public function getIni()
	{
		$grade_lists = $this->getAll();
		foreach ($grade_lists as $key => $value){
			$grade[$value['grade_id']] = $value['grade_name'];
		}

		return $grade;
	}

	/** 读取列表 **/
	public function getAll($field = "*")
	{
		$grade_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_grade_up` WHERE is_now='1' ORDER BY grade_code ASC");

		return $grade_lists;
	}
	/** 通過id年级历史表(最后id，代码) **/
	public function getGradeUpinfo($grade_id, $field = "*")
	{
		$upg_list = $this->db->fetchRow("SELECT Max(upg_id),grade_code FROM `tbl_grade_up` WHERE grade_id = {$grade_id}");
		$upg_list['upg_id'] = $upg_list['Max(upg_id)'];
		return $upg_list;
	}
	/** 通过代码找年级 **/
	public function getUpCode($grade_code, $field = "*")
	{
		$grade_data = $this->db->fetchAll("SELECT {$field} FROM `tbl_grade_up` WHERE grade_code='{$grade_code}' AND upg_graduate ='1'");

		return $grade_data;
	}
	/** 判断上年级是否升迁 **/
	public function getUpGraduate($grade_code, $field = "*")
	{
		
		$grade_data = $this->db->fetchAll("SELECT {$field} FROM `tbl_grade_up` WHERE grade_code ='{$grade_code}' AND is_now ='1'");

		return $grade_data;
	}
	/** 查找最大Code **/
	public function getMaxCode( $field = "*")
	{
		$grade_data = $this->db->fetchRow("SELECT Max(grade_code) FROM `tbl_grade_up` WHERE is_now ='1'");
		$grade_data['grade_code'] = $grade_data['Max(grade_code)'];
		return $grade_data;
	}

	/** id得到年级历史数据 **/
	public function getUpInfo($grade_id, $field = "*")
	{
		$up_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_grade_up` WHERE grade_id='{$grade_id}' AND is_now ='1'");

		return $up_data;
	}
	/** 得到年级历史 （ 当前状态 ）数据 **/
	public function getUpAll( $field = "*")
	{
		$up_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_grade_up` WHERE  is_now ='1'");

		return $up_lists;
	}
	/** 得到年级数据 **/
	public function getInfo($grade_id, $field = "*")
	{
		$grade_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_grade_up` WHERE grade_id='{$grade_id}' AND is_now ='1'");

		return $grade_data;
	}
	/** 通过代码找年级 **/
	public function getCode($grade_code, $field = "*")
	{
		$grade_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_grade` WHERE grade_code='{$grade_code}' ");

		return $grade_data;
	}

	/** 读取名称根据id **/
	public function getNameById($grade_id)
	{
		$data = $this->getInfo($grade_id, "grade_name");

		return $data['grade_name'];
	}
	/** 获取年级毕业表信息 **/
	public function getGraduateAll($field = "*")
	{
		$graduate_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_grade_graduate`");

		return $graduate_lists;
	}
	/** id，查看是否毕业 **/
	public function getGraduate($grade_id,$field = "*")
	{
		$data = $this->db->fetchRow("SELECT {$field} FROM `tbl_grade_graduate` WHRER grade_id='{$grade_id}' AND is_show ='0' AND upg_graduate = '1'");

		return $data;
	}
	/** 获取年级毕业表信息 **/
	public function getGraduateinfo($graduate_id, $field = "*")
	{
		$graduate_data = $this->db->fetchRow("SELECT {$field} FROM `tbl_grade_graduate` WHERE graduate_id = {$graduate_id}");

		return $graduate_data;
	}
	/** 畢業不滿足的條件 **/
	public function getNoRecall($grade_id, $field = "*")
	{
		$row = $this->db->fetchRow("SELECT {$field} FROM `tbl_grade_up` WHERE grade_id='{$grade_id}' AND upg_graduate ='1'");
		
		return $grade_data;
	}
}

?>